package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.entity.Related;
import com.ruoyi.system.entity.SysRemind;
import com.ruoyi.system.mapper.RelatedMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.ViewConfigPushMesMapper;
import com.ruoyi.system.service.RelatedService;
import com.ruoyi.system.service.SysRemindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 相关报告信息(Related)表服务实现类
 *
 * @author makejava
 * @since 2023-02-07 11:26:31
 */
@Service("relatedService")
public class RelatedServiceImpl implements RelatedService {


    @Autowired
    private SysRemindService sysRemindService;
    @Resource
    private EnterpriseWXServiceImpl enterpriseWXService;

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private RelatedMapper relatedDao;

    @Resource
    private ViewConfigPushMesMapper viewConfigPushMesMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Related queryById(Integer id) {
        return this.relatedDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param related     筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Related> queryByPage(Related related, PageRequest pageRequest) {
        long total = this.relatedDao.count(related);
        return new PageImpl<>(this.relatedDao.queryAllByLimit(related, pageRequest), pageRequest, total);
    }

    @Override
    public List<Related> queryAll(Related related) {
        List<Related> relateds = relatedDao.queryAll(related);
        if (!CollectionUtils.isEmpty(relateds)) {
            for (Related var : relateds) {
                if (null == var.getRemind()) {
                    var.setColor(0);
                    continue;
                }
                //获取当前时间
                String toDateStr = DateUtils.getDate();
                Date toDate = new Date();

                //获取提醒日期
                String remind = var.getRemind();
                Date date = DateUtils.parseDate(remind);

                if (toDateStr.equals(remind)) {
                    var.setColor(1);
                    continue;
                }

                boolean after = toDate.after(date);
                if (after) {
                    var.setColor(2);
                    continue;
                }

                var.setColor(0);
            }
        }
        return relateds;
    }

    /**
     * 新增数据
     *
     * @param related 实例对象
     * @return 实例对象
     */
    @Override
    public Related insert(Related related) {
        related.setCreatedDate(DateUtils.getNowDate());
        this.relatedDao.insert(related);
        return related;
    }

    /**
     * 修改数据
     *
     * @param related 实例对象
     * @return 实例对象
     */
    @Override
    public Related update(Related related) {
        this.relatedDao.update(related);
        return this.queryById(related.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.relatedDao.deleteById(id) > 0;
    }

    @Scheduled(cron = "0 0 8 * * ?")
    public void checkRelated() {
        Related related = new Related();
        List<Related> relateds = relatedDao.queryAll(related);
        if (CollectionUtils.isEmpty(relateds)) {
            return;
        }

        for (Related var : relateds) {
            String remind = var.getRemind();
            Date date = new Date();
            boolean after = date.after(DateUtils.parseDate(remind));
            if (after) {
                String stringBuffer =
                        "报告名称:" + var.getReportsName() +
                                "编号:" + var.getSerialNumber() +
                                "即将到期。";

               /* ViewConfigPushMes viewConfigPushMes = new ViewConfigPushMes();
                viewConfigPushMes.setViewName("相关报告信息");
                List<ViewConfigPushMes> configPushMes = viewConfigPushMesMapper.queryByViewKey(viewConfigPushMes);
                if (!CollectionUtils.isEmpty(configPushMes)) {
                    ViewConfigPushMes pushMes = configPushMes.stream().findFirst().orElse(new ViewConfigPushMes());
                    if (StringUtils.isNotEmpty(pushMes.getViewValue())) {
                        boolean contains = pushMes.getViewValue().contains(",");
                        if (contains) {
                            String[] split = pushMes.getViewValue().split(",");
                            for (String vars : split) {
                                sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, Integer.valueOf(vars));
                            }
                        } else {
                            sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, Integer.valueOf(pushMes.getViewValue()));
                        }
                    }
                }*/

                SysRole sysRole = new SysRole();
                sysRole.setRoleKey("qyzzts");
                List<SysUser> sysUsers = sysUserMapper.selectUserByRoleName(sysRole);
                if (CollectionUtils.isEmpty(sysUsers)) {
                    return;
                }

                for (SysUser sys : sysUsers) {
                    sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, sys.getUserId().intValue());
                }

                List<String> collect = sysUsers.stream().map(SysUser::getWxUserId).collect(Collectors.toList());
                EnterpriseWXServiceImpl.SendMessageRequest sendMessageRequest = new EnterpriseWXServiceImpl.SendMessageRequest();
                sendMessageRequest.setTouser(CollUtil.join(collect, "|"));
                sendMessageRequest.setMsgtype("text");
                sendMessageRequest.setAgentid(1000319);
                EnterpriseWXServiceImpl.MessageContent messageContent = new EnterpriseWXServiceImpl.MessageContent();
                messageContent.setContent(stringBuffer);
                sendMessageRequest.setText(messageContent);
                enterpriseWXService.pushMessage(sendMessageRequest);
            }

        }
    }


}
